import {
  ICodeStruct,
  BuilderComponentPlugin,
  BuilderComponentPluginFactory,
  // FileType,
  // ChunkType,
  // IContainerInfo,
  ICodeChunk,
  COMMON_CHUNK_NAME,
  // CLASS_DEFINE_CHUNK_NAME,
  // DEFAULT_LINK_AFTER,
} from '@alilc/lowcode-code-generator';

export interface PluginConfig {
  fileType: string;
}

const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (
  // config?
) => {
  // const cfg: PluginConfig = {
  //   fileType: FileType.JSX,
  //   ...config,
  // };

  const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
    const next: ICodeStruct = {
      ...pre,
    };

    // const ir = next.ir as IContainerInfo & {
    //   dataSourcesTypes: any;
    //   methodsModule?: {
    //     type?: 'JSModule';
    //     source?: string;
    //   };
    // };

    const chunks = next.chunks as ICodeChunk[];
    // 找到外部引入资源createAxiosRequestHandler方法的chunk
    const createAxiosRequestHandlerChunk = chunks.find((chunk: ICodeChunk) => chunk.name === COMMON_CHUNK_NAME.ExternalDepsImport && chunk.content.includes('@alilc/lowcode-datasource-axios-handler'));
    if (createAxiosRequestHandlerChunk) {
      // 将其内容引入的@alilc/lowcode-datasource-axios-handler依赖改为@/shared/utils
      createAxiosRequestHandlerChunk.content = createAxiosRequestHandlerChunk.content.replace('@alilc/lowcode-datasource-axios-handler', '@/shared/utils');
    }
    return next;
  };

  return plugin;
};

export default pluginFactory;
